package com.xx.sg.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xx.sg.own.entity.CustomField;
import com.xx.sg.own.service.CustomFieldService;
import com.xx.sg.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/customField")
@Tag(name = "自定义属性", description = "自定义属性")
public class CustomFieldController {

    @Value("${sg.profile}")
    private String profile;

    @Autowired
    private CustomFieldService customFieldService;


    @GetMapping("/customFieldList")
    @Operation(summary = "自定义列表查询", description = "自定义列表查询")
    private R customFieldList(@RequestParam(name = "facilityId") String facilityId,
                              @RequestParam(name = "facilityType") String facilityType) {
        LambdaQueryWrapper<CustomField> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomField::getFacilityId, facilityId)
                .eq(CustomField::getFacilityType, facilityType);
        return R.success(customFieldService.list(wrapper));
    }

    @PostMapping("/add")
    @Operation(summary = "自定义列表查询", description = "自定义列表查询")
    public R addCustomField(@RequestBody List<CustomField> list) {

        if (list != null && list.size() > 0) {
            LambdaQueryWrapper<CustomField> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CustomField::getFacilityId, list.get(0).getFacilityId())
                    .eq(CustomField::getFacilityType, list.get(0).getFacilityType());
            customFieldService.remove(wrapper);
            customFieldService.saveBatch(list);
        }
        return R.success();
    }


}

